package code._20stream._03midOperations;

import code._20stream.bean.Student;
import code._20stream.bean.StudentList;
import org.junit.Test;

import java.sql.SQLOutput;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class MidOperationTest {
    @Test
    public void testFilter(){
        List<Student> studentList = new StudentList().studentList;
        List<Student> students = studentList.stream()
                .filter(new Predicate<Student>() {
                    @Override
                    public boolean test(Student student) {
                        return student.getAddress() == Student.Address.BJ;
                    }
                })
                .collect(Collectors.toList());
        System.out.println(students);
        System.out.println(students.size());
    }

    @Test
    public void testFilter2(){
        List<Student> studentList = new StudentList().studentList;
        List<Student> students = studentList.stream()
                .filter(new Predicate<Student>() {
                    @Override
                    public boolean test(Student student) {
                        return student.getAddress() == Student.Address.BJ && student.getAge() > 20;
                    }
                })
                .collect(Collectors.toList());
        System.out.println(students);
        System.out.println(students.size());
    }

    @Test
    public void testFilter3(){
        List<Student> studentList = new StudentList().studentList;
        List<Student> students = studentList.stream()
                .filter(new Predicate<Student>() {
                    @Override
                    public boolean test(Student student) {
                        return student.getAddress() == Student.Address.BJ;
                    }
                })
                .filter(new Predicate<Student>() {
                    @Override
                    public boolean test(Student student) {
                        return student.getAge() > 20;
                    }
                })
                .collect(Collectors.toList());
        System.out.println(students);
        System.out.println(students.size());
    }

    @Test
    public void testDistinct(){
        List<Student> studentList = new StudentList().studentList;
        List<Student> students = studentList.stream()
                .filter(student -> student.getAddress() == Student.Address.BJ)
                .distinct()
                .collect(Collectors.toList());
        System.out.println(students);
        System.out.println(students.size());
    }

    @Test
    public void testLimit(){
        List<Student> studentList = new StudentList().studentList;
        List<Student> students = studentList.stream()
                .filter(student -> student.getAddress() == Student.Address.BJ)
                .distinct()
                .sorted(new Comparator<Student>() {
                    @Override
                    public int compare(Student o1, Student o2) {
                        return o2.getHeight()-o1.getHeight();
                    }
                })
                .limit(3)
                .collect(Collectors.toList());
        System.out.println(students);
        System.out.println(students.size());
    }

    @Test
    public void testMap(){
        List<Student> studentList = new StudentList().studentList;
        List<String> names = studentList.stream()
                .filter(student -> student.getAddress() == Student.Address.BJ)
                .map(new Function<Student, String>() {
                    @Override
                    public String apply(Student student) {
                        return student.getName();
                    }
                })
                .distinct()
                .collect(Collectors.toList());
        System.out.println(names);
        System.out.println(names.size());
    }

    @Test
    public void testMap2(){
        List<Student> studentList = new StudentList().studentList;
        List<String> names = studentList.stream()
                .filter(new Predicate<Student>() {
                    @Override
                    public boolean test(Student student) {
                        return student.getAddress() == Student.Address.BJ;
                    }
                })
                .map(new Function<Student, String>() {
                    @Override
                    public String apply(Student student) {
                        return student.getName();
                    }
                })
                .collect(Collectors.toList());
        System.out.println(names);
        System.out.println(names.size());
    }

    @Test
    public void testMap3(){
        List<Student> studentList = new StudentList().studentList;
        List<String> names = studentList.stream()
                .filter(student -> student.getAddress() == Student.Address.BJ)
                .map(Student::getName)
                .map(name -> name.substring(0, 1))
                .distinct()
                .collect(Collectors.toList());
        System.out.println(names);
        System.out.println(names.size());
    }

    @Test
    public void testMap4(){
        List<Student> studentList = new StudentList().studentList;
        List<Integer> ages = studentList.stream()
                .filter(student -> student.getAddress() == Student.Address.BJ)
                .map(Student::getAge)
                .collect(Collectors.toList());
        System.out.println(ages);
        System.out.println(ages.size());
    }

    @Test
    public void testMap5(){
        List<Student> studentList = new StudentList().studentList;
        List<String> result = studentList.stream()
                .map(Student::getAge)
                .map(age -> {
                    if (age >= 18 && age <= 20) {
                        return "年轻人";
                    } else if (age > 20 && age < 24) {
                        return "中年人";
                    } else {
                        return "老年人";
                    }
                })
                .collect(Collectors.toList());
        System.out.println(result);
        System.out.println(result.size());
    }

    @Test
    public void testSorted(){
        List<Student> studentList = new StudentList().studentList;
        List<Integer> ages = studentList.stream()
                .map(Student::getAge)
                .sorted()
                .collect(Collectors.toList());
        System.out.println(ages);
        System.out.println(ages.size());
    }

    @Test
    public void testSorted2(){
        List<Student> studentList = new StudentList().studentList;
        List<Student> students = studentList.stream()
                .filter(student -> student.getAddress() == Student.Address.BJ)
                .sorted(new Comparator<Student>() {
                    @Override
                    public int compare(Student o1, Student o2) {
                        return o2.getHeight() - o1.getHeight();
                    }
                })
                .collect(Collectors.toList());
        System.out.println(students);
        System.out.println(students.size());
    }
}
